/**
 * @author rjimenez
 */

/*
 * Check loading * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 */

function calculateChromosomeBandDrawRequestNumber(){
	var isNullKaryotypeFeatures = YAHOO.lang.isNull(YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation);
	var repNumberChromosome = 1;
	var repNumberKaryotype = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.selectedEntryPoints.ids.length;
	if(isNullKaryotypeFeatures){
		var repTotal = 0
	} else {
		var repTotal = repNumberChromosome + repNumberKaryotype;
	}
	return repTotal 
}

function calculateAnnotationDrawRequestNumber(){
	var isNullKaryotypeFeatures = YAHOO.lang.isNull(YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation);
	var repTotal = 0;
	/*
	if (isNullKaryotypeFeatures) {
		var repNumberBand = 0;
	} else {
		var repNumberBand = 1;
	}
	*/
	repNumberZoom = 0;
	repNumberBand = 0;
	
	var repNumberChromosome = 1;
	//var repNumberSources = YAHOO.dasBrowser.genome.annotations.sources.length;
	
	if (YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.showKaryotype == false) {
		var repNumberKaryotype = 0;
	} else {
		var repNumberKaryotype = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.selectedEntryPoints.ids.length;
		var annotationSources = YAHOO.dasBrowser.genome.annotations.sources;
		for (var j = 0; j < annotationSources.length; j++) {
			switch(annotationSources[j].panel){
				case "karyotype":
				  repTotal = repTotal + (repNumberZoom + repNumberBand + repNumberChromosome + repNumberKaryotype);
				  break;    
				case "chromosome":
				  repTotal = repTotal + (repNumberZoom + repNumberBand + repNumberChromosome);
				  break;
				case "band":
				  repTotal = repTotal + (repNumberZoom + repNumberBand);
				  break;
				case "zoom":
				  repTotal = repTotal + (repNumberZoom);
				  break;
				default:
				  //console.log("Warning! ... calculateAnnotationDrawRequestNumber did not find a proper case");
				  // Just in case I set the karyotype behavavior
				  repTotal = repTotal + (repNumberZoom + repNumberBand + repNumberChromosome + repNumberKaryotype);
			}
		}
	}
	//repTotal = (repNumberBand + repNumberChromosome + repNumberKaryotype) * repNumberSources;
	return repTotal 
	//YAHOO.dasBrowser.genome.request.drawing.chromosomeBands.total = repNumberBand + repNumberChromosome + repNumberKaryotype;
}

YAHOO.dasBrowser.genome.request.drawing.isCompleted = function(){
	//console.log("- - - - - - - ");
	if (YAHOO.dasBrowser.genome.request.drawing.allCompleted) {
	//if (!console) {
		// Already completed!
	}
	else {
		var isNullKaryotypeFeatures = YAHOO.lang.isNull(YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation);
		/*
		if (isNullKaryotypeFeatures) {
			var repNumberBand = 0;
		}
		else {
			var repNumberBand = 1;
		}
		*/
		var bandRequests = YAHOO.dasBrowser.genome.request.drawing.chromosomeBands.total;
		var annotationRequests = YAHOO.dasBrowser.genome.request.drawing.annotations.total;
	
		/*
		if (YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.showKaryotype == false) {
			var repNumberBand = 0;
		}
		*/
		var repNumberBand = 0;
		
		var repNumberChromosome = 1;
		
		var totalRequests = bandRequests + annotationRequests;	
		
		var bandRequestsCompleted = YAHOO.dasBrowser.genome.request.drawing.chromosomeBands.completed;
		var annotationRequestsCompleted = YAHOO.dasBrowser.genome.request.drawing.annotations.completed;
		var segmentSourcesFailed = YAHOO.dasBrowser.genome.annotations.segmentSourcesFailed;
		var totalRequestsCompleted = bandRequestsCompleted + annotationRequestsCompleted + (segmentSourcesFailed + repNumberBand + repNumberChromosome);
		
		/*
		console.log("bandRequestsCompleted: "+bandRequestsCompleted);
		console.log("annotationRequestsCompleted: "+annotationRequestsCompleted);
		console.log("segmentSourcesFailed: "+segmentSourcesFailed);
		console.log("repNumberBand: "+repNumberBand);
		console.log("repNumberChromosome: "+repNumberChromosome);
		
		console.log("totalRequests: "+totalRequests);
		console.log("totalRequestsCompleted: " + totalRequestsCompleted);
		*/
		
		
		if (totalRequests == totalRequestsCompleted) {
			YAHOO.dasBrowser.genome.request.drawing.allCompleted = true;
			//console.log("completed!!!");
			YAHOO.example.container.wait.hide();
			YAHOO.dasBrowser.genome.request.doAtTheEndOfLoading();
		}
	}
}

function displayLoadingWindow(showLoadingWindow){
	YAHOO.example.container.wait = 
    new YAHOO.widget.Panel("wait",  
        { width: "240px", 
            fixedcenter: true, 
            close: false, 
            draggable: false, 
            zindex:4,
            modal: true,
            visible: false
        } 
    );
    YAHOO.example.container.wait.setHeader("Loading, please wait...");
    YAHOO.example.container.wait.setBody("<img src=\"img/rel_interstitial_loading.gif\"/>");
    YAHOO.example.container.wait.render(document.body);
	if(showLoadingWindow){
		YAHOO.example.container.wait.show();	
	}
}

	